package com.neuedu.his.dao;


import com.neuedu.his.common.utils.DBUtils;
import com.neuedu.his.entity.Upload;

import java.util.List;
import java.util.Map;

public class UploadDao {

    /**
     * 查询集合
     */
    public List<Upload> selectList(String where , Object ... params){
        String sql =" select * from upload ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        List<Upload> uploads = DBUtils.selectList(Upload.class, sql, params);

        return uploads;
    }

    /**
     * 分页查询
     * @param where
     * @param pageNo
     * @param size
     * @param params
     * @return
     */
    public List<Upload> selectPage(String where , int pageNo,int size ,Object ... params){
        String sql =" select * from upload ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql += " limit "+(pageNo-1)*size+"   ,"+size;

        System.out.println("sql = " + sql);
        List<Upload> uploads = DBUtils.selectList(Upload.class, sql, params);

        return uploads;
    }


    /**
     * 查询单条结果
     */
    public Upload selectOne(String where ,Object ... params){
        String sql =" select * from upload ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(Upload.class, sql, params);
    }


    /**
     * 查询总条数
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from upload ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }




    /**
     * 根据数据进行插入
     * @param upload
     * @return
     */
    public int insert(Upload upload){
        String sql = "insert into upload(       ext   ,       origin_name   ,       path   ,       up_time   ,       up_type   ,       up_user   ) values(   ?   ,     ?   ,     ?   ,     ?   ,     ?   ,     ?    )";
        int count = DBUtils.update( sql,    upload.getExt()    ,     upload.getOriginName()    ,     upload.getPath()    ,     upload.getUpTime()    ,     upload.getUpType()    ,     upload.getUpUser() );
    String getKeySql = "SELECT MAX(upload_id) id FROM upload   ";
    Map<String,Integer>map =  DBUtils.selectMap(getKeySql);
    upload.setUploadId(map.get("id"));
        return count;
    }


    /**
     * 根据主键更新一条
     * @param upload
     * @return
     */
    public int updateById(Upload upload){
        String sql = " update upload set     ext = ?   ,       origin_name = ?   ,       path = ?   ,       up_time = ?   ,       up_type = ?   ,       up_user = ?   where  upload_id = ?  ";

        return DBUtils.update( sql,
                upload.getExt()  ,
                upload.getOriginName()  ,
                upload.getPath()  ,
                upload.getUpTime()  ,
                upload.getUpType()  ,
                upload.getUpUser()  ,
                upload.getUploadId() );
    }



    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " delete from upload  where upload_id = ?";
        return DBUtils.update( sql, id);

    }


}